package com.hmall.item.web;



import com.hmall.common.client.ClientFeign;
import com.hmall.common.contans.MqContants;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.dto.item.ItemDTO;
import com.hmall.common.pojo.item.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;


@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 商品分页查询
     * @param page 当前页
     * @param size 页大小
     * @return
     */
    @GetMapping("/list")
    public PageDTO<Item> itemList(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        return itemService.itemList(page, size);
    }

    /**
     * 根据商品id查询商品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item seleceById(@PathVariable("id") Long id) {
        return itemService.getById(id);
    }

    /**
     * 新增商品
     */
    @PostMapping
    public void addItem(@RequestBody ItemDTO itemDTO) {
        itemService.addItem(itemDTO);
    }

    //商品上下架
    @PutMapping("/status/{id}/{status}")
    public void updataItem(@PathVariable Long id, @PathVariable Integer status) {
        //LambdaQueryWrapper<Item> wrapper = Wrappers.lambdaQuery();
        //wrapper.eq(Item::getId,id).eq(Item::getStatus,status);
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        itemService.updateById(item);
        if (status==2){
            //mq 删除
            // 交换机名称
            String exchangeName = MqContants.HOTEL_EXCHANGE;
            // 发送消息
            rabbitTemplate.convertAndSend(exchangeName, MqContants.HOTEL_DELETE_KEY,id);
        }else if (status==1){
            //mq  添加
            // 交换机名称
            String exchangeName = MqContants.HOTEL_EXCHANGE;
            // 消息
            // 发送消息
            rabbitTemplate.convertAndSend(exchangeName, MqContants.HOTEL_INSERT_KEY, id);
        }

    }

    //修改商品
    @PutMapping
    public void updata(@RequestBody ItemDTO itemDTO) {
        itemService.updateItem(itemDTO);
    }

    //根据id删除商品
    @DeleteMapping("/{id}")
    public void deleteItem(@PathVariable Long id) {
        itemService.removeById(id);
    }

    /**
     * 扣减库存
     * @param itemId
     * @param num
     */
    @PutMapping("/updateInventory/{itemId}/{num}")
    public void updateInventory(@PathVariable("itemId") Long itemId,
                                @PathVariable("num") Integer num) {
        Item item = seleceById(itemId);
        item.setStock(num);
        itemService.updateById(item);
    }
}
